<?php

require_once('getDBConnection.php');
require_once('Profile.php');



	class ProfileService{

		private function getConnection(){
			$obj = new DBConnection();
			return($obj->getDBC());
		}

		/*Will save a profile*/
		function saveProfile($profile){
			$mysqli = $this->getConnection();






			$mysqli->close();
		}

		/*Will delete a single favorite food from the favorite food table based on userId and foodId*/
		function deleteProfile($userId, $foodId){
			$mysqli = $this->getConnection();

			$mysqli->close();
		}

		/*given a userId this function gathers all food in the favorite food table for the user and returns it*/
		function getFullProfile($userId, $date){
			$mysqli = $this->getConnection();

			$query = "SELECT " .
				"p." . Constants::USER_ID . ", " .
				"p." . Constants::USER_NAME . ", " .
				"p." . Constants::FIRST_NAME . ", " .
				"p." . Constants::LAST_NAME . ", " .
				"p." . Constants::GENDER . ", " .
				"p." . Constants::BIRTHDATE . ", " .
				"p." . Constants::EMAIL . ", " .
				"p." . Constants::WEIGHT . ", " .
				"p." . Constants::HEIGHT . ", " .
				"p." . Constants::GOAL_WEIGHT . ", " .
				"p." . Constants::GOAL_DATE . ", " .
				"p." . Constants::POINTS_ID . ", " .
				"pts." . Constants::DAILY_PTS . ", " .
				"pts." . Constants::WEEKLY_PTS . ", " .
				"pts." . Constants::WEEKLY_PTS_REM . ", " .
				"pts." . Constants::ACTIVITY_PTS . ", " .
				"pts." . Constants::ACTIVITY_PTS_REM . ", " .
				"pts." . Constants::START_DATE . ", " .
				"pts." . Constants::END_DATE .
				" FROM " . Constants::PROFILE_TABLE . " p, " . Constants::POINTS_TABLE . " pts" .
				" WHERE p." . Constants::USER_ID . " = $userId " .
				" AND p." . Constants::POINTS_ID . " = pts." . Constants::POINTS_ID;

				if($date == null){
					$query .= " and pts." . Constants::END_DATE . " is null;";
				}else{
					$query .= " and pts." . Constants::START_DATE . " < '" . $date . "' AND '" . $date . "' <  ". Constants::END_DATE . ";";
				}



			$data = $mysqli->query($query);

			if($mysqli->error){
				die("An error occured while trying to gather your profile information. The error message given is: $mysqli->error");
			}

			if ($data->num_rows == 1) {
				  $row = $data->fetch_assoc();

				  $return = "<profile>";//start the return value

							$return .=	"<userId>".$row[Constants::USER_ID]."</userId>".
										"<name>".$row[Constants::USER_NAME]."</name>".
										"<firstName>".$row[Constants::FIRST_NAME]."</firstName>".
										"<lastName>".$row[Constants::LAST_NAME]."</lastName>".
										"<gender>".$row[Constants::GENDER]."</gender>".
										"<birthdate>".$row[Constants::BIRTHDATE]."</birthdate>".
										"<email>".$row[Constants::EMAIL]."</email>".
										"<weight>".$row[Constants::WEIGHT]."</weight>".
										"<height>".$row[Constants::HEIGHT]."</height>".
										"<goalWeight>".$row[Constants::GOAL_WEIGHT]."</goalWeight>".
										"<goalDate>".$row[Constants::GOAL_DATE]."</goalDate>".
										"<pointsId>".$row[Constants::POINTS_ID]."</pointsId>".
										"<dailyPoints>".$row[Constants::DAILY_PTS]."</dailyPoints>".
										"<weeklyPoints>".$row[Constants::WEEKLY_PTS]."</weeklyPoints>".
										"<weeklyPointsRemaining>".$row[Constants::WEEKLY_PTS_REM]."</weeklyPointsRemaining>".
										"<activityPoints>".$row[Constants::ACTIVITY_PTS]."</activityPoints>".
										"<activityPointsRemaining>".$row[Constants::ACTIVITY_PTS_REM]."</activityPointsRemaining>";


	       		 $return .= "</profile>";
	       		$data->close();
	 			$mysqli->close();
		 		return($return);
			}
			$data->close();
 			$mysqli->close();
			return(null);

		}


		/*This function is used to return only data relavent to a user logging into the system and nothing more.*/
		function getLoginProfile($user, $password){
			$mysqli = $this->getConnection();
			$query = "SELECT " .
				Constants::USER_ID . ", " .
				Constants::USER_NAME .
				" FROM " . Constants::PROFILE_TABLE .
				" WHERE " . Constants::USER_NAME . " = '$user' AND " . Constants::PASSWORD . " = SHA('$password');";

			$data = $mysqli->query($query);

			if($mysqli->error){
				die("An error occured while trying to log in please try not to hack the system or enter invalid log in information least hostile action be required. \n" .
				"The error message given is: $mysqli->error");
			}

			if ($data->num_rows == 1) {
				  // The log-in is OK so set the user ID and username session vars (and cookies), and redirect to the home page
				  $row = $data->fetch_assoc();

				  $profile = new Profile();
				  $profile->userId = $row[Constants::USER_ID];
				  $profile->username = $row['username'];
			}else{
				$profile = null;
			}
			$data->close();
			$mysqli->close();
			return($profile);//returns a profile with just the userId and username in it

		}

	}

?>